#include "CCreditsScreen.h"
#include "cmath"

void CCreditsScreen::load(CGraphics *gl)
{
    // tekstūrų užloadinimas

    font = QFont("Verdana", 15, 0, 0);
    fontCr = QFont("Verdana", 15, 0, true);

    labels.append( QString::fromStdWString(L":: Apie kūrėjus ::") );
    labels.append( QString::fromStdWString(L"KTU IF-8/5 grupės studentai:") );
    labels.append( QString::fromStdWString(L"Ugnius Kavaliauskas") );
    labels.append( QString::fromStdWString(L"Benediktas Knispelis") );
    labels.append( QString::fromStdWString(L"(C) 2010") );

    selection = -1;
    buttons.append( TMLabel( QString::fromStdWString(L"Grįžti"), 50, 450, font, 0 ) );
    fader = 1.0f;
    lUfade = lBfade = 0.0f;
}

void CCreditsScreen::unload(CGraphics *gl)
{

}

int CCreditsScreen::update(CInput *in)
{
    int action = -1;

    if ( in->wasKeyPressed(Qt::Key_Return) )    // Enter
        action = selection;

    if ( in->wasButtonPressed(Qt::LeftButton) )    // pelės paspaudimas
        foreach ( TMLabel l, buttons ) {
            int Mx = in->getCursorPos().x();
            int My = in->getCursorPos().y();
            if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 )
                action = l.action;
        }

    if ( in->hasMouseMoved() )           // pelės žymėjimas
        foreach ( TMLabel l, buttons ) {
            int Mx = in->getCursorPos().x();
            int My = in->getCursorPos().y();
            if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 )
                selection = l.action;
        }

    if( ( in->wasKeyPressed(Qt::Key_Down) || in->wasKeyPressed(Qt::Key_Right) )
        && selection < buttons.size()-1 ) {  //  žymėjimas klaviatūra
        selection++;
    } else if( ( in->wasKeyPressed(Qt::Key_Up) || in->wasKeyPressed(Qt::Key_Left) )
        && selection > 0 ) {
        selection--;
    }

    if ( in->wasKeyPressed(Qt::Key_Escape) )
        action = 0; // Grįžti

    if ( action == 0 ) {
        CScreen *sc;
        sc = sm->getScreen("testScreen");
        if ( sc != 0 ) sc->setHidden(false);
        sc = sm->getScreen("gamePauseScreen");
        if ( sc != 0 ) sc->setHidden(false);
        sc = sm->getScreen("menuScreen");
        if ( sc != 0 ) sc->setHidden(false);
        sm->removeScreen( this );
    }

    for ( int i = 0; i < buttons.size(); i++ ) {
        if ( i == selection ) {
            buttons[i].fade += 0.2;
            if ( buttons[i].fade > 2.0 ) buttons[i].fade = 2.0;
        } else {
            if ( buttons[i].fade > 0.0 ) {
                buttons[i].fade -= 0.1;
                if ( buttons[i].fade < 0.0 ) buttons[i].fade = 0.0;
            }
        }
    }

    return 0;
}

void CCreditsScreen::draw(CGraphics *gl)
{
    glLoadIdentity();
    glColor3f(1.0f, 1.0f, 1.0f );

    if ( sm->getScreen("backgroundScreen") == 0 ) { // nepiešti jei fone yra backgroundScreen
        glDisable(GL_TEXTURE_2D);       // piešiamas pusiau permatomas juodas ekranas
        glColor4f(0.0f, 0.0f, 0.0f, 0.5f );
        glBegin(GL_QUADS);  // fonas
            glVertex2i(  0, 0 );
            glVertex2i(  0, 500 );
            glVertex2i(700, 500 );
            glVertex2i(700, 0 );
        glEnd();
        glEnable(GL_TEXTURE_2D);
    }

    fader += 0.1f;
    lUfade = 0.85f + sin( fader )/8.0f;

    glColor4f(1.0f, 1.0f, 1.0f, 1.0f );
    gl->renderTexts( 350-75, 25, labels.first(), font);
    gl->renderTexts( 350-135, 75, labels.at(1), font );
    glColor4f( 1.0f, 0.8f, 0.4f, lUfade );
    gl->renderTexts( 350-95, 125, labels.at(2), fontCr);
    glColor4f( 1.0f, 0.8f, 0.4f, lUfade );
    gl->renderTexts( 350-95, 175, labels.at(3), fontCr);
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f );
    gl->renderTexts( 350-50, 475, labels.at(4), font);

    foreach ( TMLabel l, buttons ) {
//        if ( selection == l.action ) glColor3f( 0.5f, 0.5f, 0.5f ); else glColor3f(1.0f, 1.0f, 1.0f );
        glColor4f( 1.0f, 1.0f, 1.0f, 0.5f+0.25*l.fade );
        gl->renderTexts( l.x, l.y, l.text, font );
    }
}
